﻿/* 
 *  <copyright file="DebugConsole.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SobrietyEngine.Events;
using SobrietyEngine.Events.Logging;

namespace SobrietyEngine.Screens
{
    public class DebugConsole : BaseScreen, IEventListener
    {
        //CreateDebugViews window
        //position, font color, lis of logged items
        List<String> lines;
        
        public int MaxLines { get; set; }

        Vector2 UpperRightPos = new Vector2();
        Color color = Color.White;
        
        public DebugConsole()
        {
            lines = new List<string>();
            MaxLines = 10;
            Overlay = true;
        }

        public override void Initialize()
        {
            base.Initialize();
            Popup = true;
        }

        public override void RegisterEventListeners()
        {            
            EventRouter.AddEventListener(typeof(LoggedItem), this);
            EventRouter.AddEventListener(typeof(Events.Screens.ToggleDebugConsole), this);
        }

        public override void UnregisterEventListeners()
        {
            EventRouter.RemoveEventListener(this);
        }

        public override void Hide()
        {
            base.Hide();
        }

        public override void Show()
        {
            //do nothing for show
        }

        public override void Draw(GameTime gameTime)
        {
            if (!Visible)
                return;

             Vector2 itemPosition = UpperRightPos;

            // Draw each menu entry in turn.
             ScreenManager.SpriteBatch.Begin();
             float scale = 0.5f;

             for(int i=0; i < lines.Count; ++i)
             {
                 String line = lines[i];                 

                 Vector2 origin = new Vector2(0, 0);//ScreenManager.MenuSpriteFont.LineSpacing / 2);
                 itemPosition.X = UpperRightPos.X;// -(MenuSpriteFont.MeasureString(line).X / 2);
                 ScreenManager.SpriteBatch.DrawString(MenuSpriteFont, line,
                                                      itemPosition, color, 0, origin, scale,
                                                      SpriteEffects.None, 0);
                 
                 itemPosition.Y += MenuSpriteFont.LineSpacing * scale;
             }
             ScreenManager.SpriteBatch.End();
        }

        public override void HandleEvent(Events.Event e)
        {
            if(e.Type == typeof(LoggedItem))
            {
                LoggedItem lied = (LoggedItem)e.Data;

                lines.Add(e.Time.TotalGameTime.TotalMilliseconds.ToString() + ">" + lied.LogType + ":" + lied.Message);
                if (lines.Count > MaxLines)
                    lines.RemoveRange(0,lines.Count - MaxLines);                
            }
            else if(e.Type == typeof(Events.Screens.ToggleDebugConsole))
            {
                //toggle!
                Visible = !Visible;
            }
        }
    }
}